/*
 *    Copyright (c) 2023 李伟国
 *    moyu is licensed under Mulan PSL v2.
 *    You can use this software according to the terms and conditions of the Mulan PSL v2.
 *    You may obtain a copy of Mulan PSL v2 at:
 *             http://license.coscl.org.cn/MulanPSL2
 *    THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 *    See the Mulan PSL v2 for more details.
 */

package cn.moyu.controller;

import cn.moyu.constant.EntityConstant;
import cn.moyu.entity.EntityDeleteException;
import cn.moyu.entity.KettleDatabaseInfo;
import cn.moyu.response.ResponseData;
import cn.moyu.response.SuccessResponseData;
import cn.moyu.service.IKettleDatabaseInfoService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.util.ObjectUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 数据源
 */

@RestController
@RequestMapping("kettle/database")
public class KettleDatabaseController {

    @Resource
    private IKettleDatabaseInfoService databaseInfoService;

    /**
     * 获取数据源列表
     * @param name 数据源名称
     * @return 结果
     */
    @RequestMapping(value = "list", method = RequestMethod.GET)
    public ResponseData getList(@RequestParam(value = "name", required = false) String name){
        Page<KettleDatabaseInfo> listPage = databaseInfoService.getListPage(name);
        return new SuccessResponseData(listPage);
    }

    /**
     * 保存或更新数据源
     * @param databaseInfo 数据源
     * @return 结果
     */
    @RequestMapping(value = "", method = RequestMethod.POST)
    public ResponseData save(@RequestBody @Validated KettleDatabaseInfo databaseInfo){
        boolean b = this.databaseInfoService.saveOrUpdate(databaseInfo);
        return new SuccessResponseData(b);
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public ResponseData delete(@PathVariable("id") Long id, @RequestBody @Validated KettleDatabaseInfo kettleDatabaseInfo){
        if(ObjectUtils.isEmpty(kettleDatabaseInfo.getId()) || ObjectUtils.isEmpty(id)){
            throw new EntityDeleteException("缺失关键参数");
        }
        if(!id.equals(kettleDatabaseInfo.getId())){
            throw new EntityDeleteException("删除失败，无理要求");
        }
        KettleDatabaseInfo info = this.databaseInfoService.getById(id);
        if(!kettleDatabaseInfo.equals(info)){
            throw new EntityDeleteException("删除失败，不合适");
        }
        LambdaUpdateWrapper<KettleDatabaseInfo> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(KettleDatabaseInfo::getIsDeleted, EntityConstant.TYPE_DELETE_YES);
        updateWrapper.eq(KettleDatabaseInfo::getId, id);
        boolean update = this.databaseInfoService.update(updateWrapper);
        return new SuccessResponseData(update);
    }

    /**
     * 连接测试
     * @param databaseInfo 测试数据
     * @return 结果
     */
    @RequestMapping(value = "line/test", method = RequestMethod.POST)
    public ResponseData connectTest(@RequestBody @Validated KettleDatabaseInfo databaseInfo){
        String s = databaseInfoService.connectTest(databaseInfo);
        return new SuccessResponseData(s);
    }

    /**
     * 查询列表
     * @param name 搜索名称
     * @return
     */
    @RequestMapping(value = "select", method = RequestMethod.GET)
    public ResponseData select(@RequestParam(value = "name", required = false) String name){
        LambdaQueryWrapper<KettleDatabaseInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(KettleDatabaseInfo::getName, name);
        List<KettleDatabaseInfo> list = databaseInfoService.list(queryWrapper);
        return new SuccessResponseData(list);
    }
}
